package com.bolehui.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bolehui.base.enums.UserTypeEnum;
import com.bolehui.domain.po.LoginPO;
import com.bolehui.domain.po.UserPO;
import com.bolehui.domain.po.iSys.SysUserRole;
import com.bolehui.mapper.UserMapper;
import com.bolehui.service.iSys.ISysUserRoleService;
import com.bolehui.service.impl.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName UserDetailsServiceImpl
 * @Description
 * @createTime 2024/12/9 11:33
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Service
public class UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    @Lazy
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ISysUserRoleService userRoleService;

    /**
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    public UserDetails loadUserByUsername(String userName, String password, UserTypeEnum userType) throws UsernameNotFoundException {
        // 根据用户名查询
        QueryWrapper<UserPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_type", userType);
        wrapper.eq("user_name", userName);
        List<UserPO> userInfoList = userMapper.selectList(wrapper);

        if (userInfoList.size() > 1) {
            // 如果查询结果有多条记录，抛出异常
            throw new UsernameNotFoundException("用户名重复: " + userName);
        }
        if (userInfoList.isEmpty()) {
            // 查询为空则创建
            UserPO userInfo = new UserPO();
            userInfo.setUserName(userName);
            userInfo.setNickName(userName);
            userInfo.setUserType(userType);
            userInfo.setSex("男");
            userInfo.setPassword(passwordEncoder.encode(password));
            userMapper.insert(userInfo);
            Long userId = userInfo.getUserId();
            // 注册求职者
            if (UserTypeEnum.JobHunter.equals(userInfo.getUserType())) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(Long.valueOf(UserTypeEnum.JobHunter.getValue()));
                sysUserRole.setUserId(userId);
                userRoleService.save(sysUserRole);
            }
            // 注册企业
            else {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(Long.valueOf(UserTypeEnum.Enterprise.getValue()));
                sysUserRole.setUserId(userId);
                userRoleService.save(sysUserRole);
            }
            Set<String> menuPermission = permissionService.getMenuPermission(userInfo);
            return new LoginPO(userInfo, menuPermission);
        }
        // 根据用户信息查询角色和权限信息
        Set<String> menuPermission = permissionService.getMenuPermission(userInfoList.get(0));
        return new LoginPO(userInfoList.get(0), menuPermission);
    }


}
